//
//  JSSVissionView.swift
//  WeiBoItem
//
//  Created by jss on 16/4/17.
//  Copyright © 2016年 jss. All rights reserved.
//

import UIKit
import SnapKit

class JSSVissionView: UIView {
    
    //定义一个属性用来传登录和注册按钮点击的内容(闭包)
    
    var clickRegistBtn:(()->())?
    var clickLoginBtn:(()->())?
    
  
    
 
   override init(frame: CGRect) {
        super.init(frame: frame)
    //设置界面
    setupUI()
 
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    
    func setupUI() {
        
        //可在这个自定义View初始话的时候设置
//        backgroundColor = UIColor.orangeColor()
        
        //添加circleView
        addSubview(circleView)
        
        //添加中间白色View
        addSubview(centerView)
        //添加iconView
        addSubview(IconView)
  
        //添加中间lable
        addSubview(messageLable)
        
        //添加登录按钮
        addSubview(Loginbtn)
        
        //添加注册按钮
        addSubview(RegistBtn)
     
        
        //设置中间小房子图片约束
        IconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY)
        }
       
//        IconView.translatesAutoresizingMaskIntoConstraints = false
//        
//        addConstraint(NSLayoutConstraint(item: IconView, attribute: .CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
//        addConstraint(NSLayoutConstraint(item: IconView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: 0))
       
        //给cirleView添加约束
        circleView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY)
        }
//        circleView.translatesAutoresizingMaskIntoConstraints = false
//        
//        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
//        
//        addConstraint(NSLayoutConstraint(item: circleView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: 0))
        
        //约束CenterView
        centerView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.IconView.snp_centerX)
            make.centerY.equalTo(self.IconView.snp_centerY)
        }
        
//        centerView.translatesAutoresizingMaskIntoConstraints = false
//        addConstraint(NSLayoutConstraint(item: centerView, attribute: NSLayoutAttribute.CenterX, relatedBy: .Equal, toItem: IconView, attribute: .CenterX, multiplier: 1, constant: 0))
//        
//        addConstraint(NSLayoutConstraint(item: centerView, attribute: NSLayoutAttribute.CenterY, relatedBy: .Equal, toItem: IconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
//        //messageLable的约束
        messageLable.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.circleView.snp_centerX)
            make.top.equalTo(circleView.snp_bottom).offset(20)
            make.width.equalTo(255)
        }
        
//        messageLable.translatesAutoresizingMaskIntoConstraints = false
//        addConstraint(NSLayoutConstraint(item: messageLable, attribute: .CenterX, relatedBy: .Equal, toItem: circleView, attribute: .CenterX, multiplier: 1, constant: 0))
//        
//        addConstraint(NSLayoutConstraint(item: messageLable, attribute: .Top, relatedBy: .Equal, toItem: circleView, attribute: .Bottom, multiplier: 1, constant: 20))
//        
//        addConstraint(NSLayoutConstraint(item: messageLable, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 255))
//
//        //RegistBtn约束
        
        RegistBtn.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(messageLable.snp_bottom).offset(10)
            make.leading.equalTo(messageLable.snp_leading)
            make.width.equalTo(100)
        }
//        RegistBtn.translatesAutoresizingMaskIntoConstraints = false
//        addConstraint(NSLayoutConstraint(item: RegistBtn, attribute: .Top, relatedBy: .Equal, toItem: messageLable, attribute: .Bottom , multiplier: 1, constant: 10))
//        
//        addConstraint(NSLayoutConstraint(item: RegistBtn, attribute: .Leading, relatedBy: .Equal, toItem: messageLable, attribute: .Leading, multiplier: 1, constant: 0))
//        
//        addConstraint(NSLayoutConstraint(item: RegistBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        
        
        //Loginbtn约束
        Loginbtn.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(RegistBtn.snp_top)
            make.trailing.equalTo(messageLable.snp_trailing)
            make.width.equalTo(100)
        }
//        Loginbtn.translatesAutoresizingMaskIntoConstraints = false
//        addConstraint(NSLayoutConstraint(item: Loginbtn, attribute: .Top, relatedBy: .Equal, toItem: RegistBtn, attribute: .Top , multiplier: 1, constant: 0))
//        
//        addConstraint(NSLayoutConstraint(item: Loginbtn, attribute: .Trailing, relatedBy: .Equal, toItem: messageLable, attribute: .Trailing, multiplier: 1, constant: 0))
//        
//        addConstraint(NSLayoutConstraint(item: Loginbtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
//        
        
    }
    
    
    //设置每个界面的图片和文字内容(抽取的方法 给其他控制器可以调用)
    func setVissionInfo(textTitle:String?, imageName:String?) {
        
        //判断如果textTitle  imageName 存在 调用里面的方法就不用解包了
        if let img = imageName, textW = textTitle {
            
            messageLable.text = textW
            
            IconView.image = UIImage(named: img)
        }else {
            
            //代表首页
            startAnimate()
        }
        
        
    }
    //让cicleView转动 添加动画
    private func startAnimate() {
        
        //让circleVIew显示
        circleView.hidden = false
        
        //添加核心动画
        let animate = CABasicAnimation(keyPath: "transform.rotation")
        
        animate.repeatCount = MAXFLOAT
        animate.toValue = 2 * M_PI
        animate.removedOnCompletion = false
        animate.duration = 15
        
        circleView.layer.addAnimation(animate, forKey: nil)
        
        
        
    }
    
    
    //实现注册按钮的监听事件
   func didClickRegistBtn() {
        
        clickRegistBtn!()
    }
    
    //实现登录按钮的监听事件
   func didClickLoginBtn() {
        
        clickLoginBtn!()
    }
    
    
    //懒加载 IconView(中间小房子)
   private lazy var IconView:UIImageView = {
        
        let imageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
//        visitordiscover_feed_image_house
        return imageView
    }()
   
    //懒加载 circleView
    
   private lazy var circleView:UIImageView = {
        
        let imageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        
        imageView.hidden = true
        
        return imageView
    }()
    
    //懒加载中间白块View
   private lazy var centerView:UIImageView = {
    
     let imageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        
        return imageView
        
    }()
    
    
    //懒加载lable
   private lazy var messageLable: UILabel = {
        
        let lable = UILabel(textColor: UIColor.darkGrayColor(), textFont: 15)
        //设置lable的文本
        lable.text = "噢噢噢噢哦哦哦哦哦噢噢噢噢哦哦哦噢噢噢噢噢噢噢噢哦哦哦"
        //文字居中
        lable.textAlignment = .Center
        
        //文字颜色
        //lable.textColor = UIColor.darkGrayColor()
        //字体大小
        //lable.font = UIFont.systemFontOfSize(17)
        //换行
        //lable.numberOfLines = 0
    
        return lable
        
    }()
    
    //懒加载LoginBtn
   private lazy var Loginbtn:UIButton = {
        
    let btn = UIButton(title: "登录", imageName: "common_button_white", textColor: UIColor.darkGrayColor(), textFont: 17)
    
    //添加登录按钮的监听事件
    btn.addTarget(self, action: "didClickLoginBtn", forControlEvents: UIControlEvents.TouchUpInside)
        
//        btn.setTitle("登录", forState: .Normal)
        
//        btn.setBackgroundImage(UIImage(named: "common_button_white"), forState: .Normal)
        
        return btn
    }()
    
    //懒加载RegistBtn
   private lazy var RegistBtn:UIButton = {
        
        let btn = UIButton(title: "注册", imageName: "common_button_white", textColor: UIColor.orangeColor(), textFont: 17)
    
    //添加按钮的监听事件
    btn.addTarget(self, action: "didClickRegistBtn", forControlEvents: UIControlEvents.TouchUpInside)
        
//        btn.setTitle("注册", forState: .Normal)
//        
//        btn.setBackgroundImage(UIImage(named: "common_button_white"), forState: .Normal)
        
        return btn
    }()
    
}
